# -*- coding: utf-8 -*-
# @Time:2021/04/11 22:43
# @Author:Cindy
from typing import List
import pytest
import yaml
from scripts.calculator import Calculator


def get_datas():
    with open("datas/data.yaml", encoding="utf-8") as f:
        datas = yaml.safe_load(f)
    return datas


@pytest.fixture()
def connectDB():
    print("连接数据库")
    yield "搜索结果"
    print("断开数据库")

@pytest.fixture(params=[('Tom', 123), ('Jerry', 456), ('lili', 789)],
                ids=['汤姆', 'Jerry', 'lili'])
def login(request):
    print("login")
    username, password = request.param
    return username, password


@pytest.fixture()
def initcal_class():
    print("\nset up")
    cal = Calculator()
    yield cal
    print("\ntear down")


@pytest.fixture(params=get_datas()["add_data"], ids=get_datas()["ids"])
def get_datas_add(request):
    return request.param


@pytest.fixture(params=get_datas()["div_data"], ids=get_datas()["ids"])
def get_datas_div(request):
    return request.param


def pytest_collection_modifyitems(session, config, items: List):
    print("这是收集所有测试用例的方法")
    print(items)
    items.reverse()
    for item in items:
        item.name = item.name.encode('utf-8').decode('unicode-escape')
        item._nodeid = item.nodeid.encode('utf-8').decode('unicode-escape')

        if "search" in item.name:
            item.add_marker(pytest.mark.search)
        elif "add" in item.name:
            item.add_marker(pytest.mark.add)


def pytest_addoption(parser):
    parser.addoption("--cmdopt", action="store")


@pytest.fixture(scope="session")
def cmdopt(pytestconfig):
    return pytestconfig.getoption("--cmdopt")
